Option Strict On
Option Explicit On 

Public Class Win
    Private cmChild As OleDb.OleDbCommand
    Private cmType As OleDb.OleDbCommand
    Private images As ArrayList
    Private j As Integer
    Private localApply As Boolean
    Private localConnection As OleDb.OleDbConnection
    Private localImageList As ImageList
    Private localStartupArray As ArrayList
    Private localTreeView As TreeView

    Public Shared Sub TypeLoad(ByVal parentId As Integer, ByVal lv As ListView, ByVal il As ImageList, ByVal oledbConnection As OleDb.OleDbConnection)
        Dim i As Integer

        Dim dr As OleDb.OleDbDataReader = Model10.Database.GetAllowedTypes(parentId, oledbConnection)
        While dr.Read
            Dim id As Integer
            Dim txt As String
            Dim img As Image

            If dr.IsDBNull(0) Then id = 0 Else id = dr.GetInt32(0)
            If dr.IsDBNull(1) Then txt = "" Else txt = dr.GetString(1)
            If dr.IsDBNull(2) Then img = fMain.imgBlank.Image Else img = Image.FromStream(New IO.MemoryStream(CType(dr(2), Byte())))

            If i > il.Images.Count - 1 Then
                il.Images.Add(img)
            Else
                il.Images(i) = img
            End If
            img = Nothing

            If i > lv.Items.Count - 1 Then
                lv.Items.Add(txt, i).Tag = id
            Else
                Dim lvi As ListViewItem = lv.Items(i)
                lvi.ImageIndex = i
                lvi.Tag = id
                lvi.Text = txt
                lvi = Nothing
            End If
            i += 1
        End While
        dr.Close() : dr = Nothing

        Dim j As Integer
        For j = lv.Items.Count - 1 To i Step -1
            lv.Items.RemoveAt(j)
        Next
        For j = il.Images.Count - 1 To i Step -1
            il.Images.RemoveAt(j)
        Next
    End Sub

    Private Sub TreeCol(ByVal treeNode As TreeNode)
        If cmChild Is Nothing Then cmChild = Model10.Database.GetChildCommand(localConnection)
        cmChild.Parameters(0).Value = treeNode.Tag
        Dim da As New OleDb.OleDbDataAdapter(cmChild)
        Dim dt As New DataTable
        da.Fill(dt)
        da.Dispose() : da = Nothing
        Dim i As Integer

        Dim dr As DataRow
        For Each dr In dt.Rows
            TreeMem(treeNode.Nodes, i, CInt(dr(0)), CInt(dr(1)), CStr(dr(2)), CStr(dr(3)), CBool(dr(4)), CBool(dr(5)))
        Next : dr = Nothing

        dt.Dispose() : dt = Nothing

        Dim k As Integer
        For k = treeNode.Nodes.Count - 1 To i Step -1
            treeNode.Nodes.RemoveAt(k)
        Next
    End Sub
    Private Sub TreeMem(ByVal treeNodes As TreeNodeCollection, ByRef i As Integer, ByVal itemId As Integer, ByVal typeId As Integer, ByVal name As String, ByVal url As String, ByVal expanded As Boolean, ByVal startup As Boolean)
        Dim tn As System.Windows.Forms.TreeNode
        If i > treeNodes.Count - 1 Then
            tn = treeNodes.Add(name)
        Else
            tn = treeNodes(i)
            tn.Text = name
        End If
        tn.Tag = itemId

        Dim f As Integer = images.IndexOf(typeId)
        If f = -1 Then
            If cmType Is Nothing Then cmType = Model10.Database.GetIconCommand(localConnection)
            cmType.Parameters(0).Value = typeId
            Dim dr As OleDb.OleDbDataReader = cmType.ExecuteReader(CommandBehavior.SingleRow)
            If dr.Read Then
                Dim img As System.Drawing.Image
                If dr.IsDBNull(0) Then
                    img = fMain.imgBlank.Image
                Else
                    Try
                        img = System.Drawing.Image.FromStream(New IO.MemoryStream(CType(dr(0), Byte())))
                    Catch ex As Exception
                    End Try
                End If
                If j > localImageList.Images.Count - 1 Then
                    localImageList.Images.Add(img)
                Else
                    localImageList.Images(j) = img
                End If
                img = Nothing
                images.Add(typeId)
                tn.ImageIndex = j
                tn.SelectedImageIndex = j
                j += 1
            Else
                Throw New Exception("Type was not defined.")
            End If
            dr.Close() : dr = Nothing
        Else
            tn.ImageIndex = f
            tn.SelectedImageIndex = f
        End If

        TreeCol(tn)
        If localApply Then
            If expanded Then tn.Expand()
            If startup Then
                If Not localStartupArray Is Nothing Then
                    Dim foo As Boolean

                    Dim t As TreeNode
                    For Each t In localStartupArray
                        If CInt(t.Tag) = CInt(tn.Tag) Then foo = True
                    Next : t = Nothing

                    If Not foo Then localStartupArray.Add(tn)
                End If
            End If
        End If
        tn = Nothing
        i += 1
    End Sub
    Public Sub TreeLoad(ByVal treeView As Windows.Forms.TreeView, ByVal imageList As Windows.Forms.ImageList, ByVal startupArray As ArrayList, ByVal itemId As Integer, ByVal isAdmin As Boolean, ByVal apply As Boolean, ByVal cn As OleDb.OleDbConnection)
        images = New ArrayList
        j = 0
        localApply = apply
        localConnection = cn
        localImageList = imageList
        localStartupArray = startupArray
        localTreeView = treeView

        Try
            treeView.BeginUpdate()
            '---------------------
            Dim i As Integer
            localImageList.Images.Clear() '...
            Dim dt As DataTable = Model10.Database.GetRootTable(itemId, isAdmin, cn)

            Dim dr As DataRow
            For Each dr In dt.Rows
                TreeMem(treeView.Nodes, i, CInt(dr(0)), CInt(dr(1)), CStr(dr(2)), CStr(dr(3)), True, False)
            Next : dr = Nothing

            dt.Dispose() : dt = Nothing
            If Not cmChild Is Nothing Then
                cmChild.Dispose()
                cmChild = Nothing
            End If
            If Not cmType Is Nothing Then
                cmType.Dispose()
                cmType = Nothing
            End If
            images = Nothing
            localConnection = Nothing
            localImageList = Nothing
            localStartupArray = Nothing
            localTreeView = Nothing

            Dim k As Integer
            For k = treeView.Nodes.Count - 1 To i Step -1
                treeView.Nodes.RemoveAt(k)
            Next
            For k = imageList.Images.Count - 1 To j Step -1
                imageList.Images.RemoveAt(k)
            Next
            '---------------------
            treeView.EndUpdate()
        Catch ex As Exception
            treeView.EndUpdate()
            Throw ex
        End Try
    End Sub

    Public Shared Sub FillBox(ByVal dr As OleDb.OleDbDataReader, ByVal imageList As ImageList, ByVal listView As ListView)
        listView.BeginUpdate()
        '----------------------------------
        imageList.Images.Clear() : imageList.Images.Add(fMain.imgBlank.Image)

        Dim i As Integer
        While dr.Read
            Dim lvi As ListViewItem
            If i = listView.Items.Count Then
                lvi = listView.Items.Add(dr.GetString(1))

                lvi.SubItems.Add(dr.GetInt32(3).ToString)
                lvi.SubItems.Add(dr.GetString(4))
                lvi.SubItems.Add(dr.GetDateTime(5).ToString)
                lvi.SubItems.Add(dr.GetString(6))
            Else
                lvi = listView.Items(i)
                lvi.Text = CStr(dr(1))

                lvi.SubItems(1).Text = dr.GetInt32(3).ToString
                lvi.SubItems(2).Text = dr.GetString(4)
                lvi.SubItems(3).Text = dr.GetDateTime(5).ToString
                lvi.SubItems(4).Text = dr.GetString(6)
            End If
            lvi.Tag = CInt(dr(0))
            If dr.IsDBNull(2) Then
                lvi.ImageIndex = 0
            Else
                imageList.Images.Add(Image.FromStream(New IO.MemoryStream(CType(dr(2), Byte()))))
                lvi.ImageIndex = imageList.Images.Count - 1
            End If

            lvi = Nothing : i += 1
        End While

        Dim j As Integer
        For j = listView.Items.Count - 1 To i Step -1
            listView.Items.RemoveAt(j)
        Next
        '----------------------------------
        listView.EndUpdate()
    End Sub
End Class
